package com.souku.finance.application.ui.invite;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseTitleBarActivity;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.souku.finance.application.utils.EdittextListenUtil;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.level.ScreenUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CalculationActivity extends BaseTitleBarActivity {

    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.et_day)
    EditText etDay;
    //    @BindView(R.id.grid_view)
//    GridView gridView;
    @BindView(R.id.tv_money)
    TextView tvMoney;

//    CalculationAdapter adapter;

//    intent.putExtra("isCurrentDeal", bean.getIsCurrentDeal().equals("1"));
//            intent.putExtra("rate", Double.parseDouble(bean.getRate()));

    boolean isCurrentDeal;
    double rate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculation);
        ButterKnife.bind(this);

//        adapter = new CalculationAdapter(this);
//        gridView.setAdapter(adapter);

        Intent intent = getIntent();
        isCurrentDeal = intent.getBooleanExtra("isCurrentDeal", false);
        rate = intent.getDoubleExtra("rate", 0.0);

        if (isCurrentDeal) {

            String s1 = etMoney.getText().toString().trim();

            String s2 = etDay.getText().toString().trim();

            if (MyTextUtil.isEmpty(s1) || Integer.parseInt(s1) == 0) {
                ToastUtil.show("请输入投资金额");
                return;
            }

            if (Integer.parseInt(s1) < 100) {
                ToastUtil.show("投资金额不得少于100元");
                return;
            }

            if (MyTextUtil.isEmpty(s2) || Integer.parseInt(s2) == 0) {
                ToastUtil.show("请输入理财期限");
                return;
            }

            getCal_interest(Integer.parseInt(s1), s2, tvMoney);

        } else {

            EdittextListenUtil.calculation2(etMoney, etDay, tvMoney, rate);

        }

        ScreenUtils.openKeybord(etDay, this);
    }


    private void getCal_interest(int money, String duration, final TextView tvMoney) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("money", money * 100 + "");
        map.put("duration", duration);
        RequestManager.getInstance().post(UrlConstact.CAL_INTEREST, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    tvMoney.setText(MyTextUtil.setMoneyToElement(JsonUtil.getString(JsonUtil.getJSONObject(response), "data")));
                } else {
                    tvMoney.setText("错误");
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    @Override
    public void internet() {

    }

}
